package net.cc.client;
  
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 

 

public class WebServiceClient {
	private Logger logger = LoggerFactory.getLogger(WebServiceClient.class);
	private Client client; 
	private static WebServiceClient webclient = new WebServiceClient();
	static String publicUrl="http://127.0.0.1:7890/HelloWorld?wsdl";
	
	public static WebServiceClient getClient(){
		return webclient;
	}
	
	private WebServiceClient(){
		logger.info("webservice构造器------------------------------------"); 
		try{  
				DynamicClientFactory factory = DynamicClientFactory.newInstance();
				this.client = factory.createClient(publicUrl); 
		}catch(Exception e){
			e.printStackTrace();
			logger.info("错误的启动构造器"+e.getMessage());
		}
	}
	
	/**
	 * 调用webservice接口的方法
	 * @param methodName
	 * @param objects
	 * @return
	 * @throws Exception
	 */
	public String invoke(String methodName,Object...objects) throws Exception{
		logger.info("webservice 准备启动客户端...............................");
		String result = null;
		try{
			if(null != this.client){
				logger.info("webservice 客户端启动成功...............................");
				Object[] res=client.invoke(methodName,objects);
				logger.info("webservice 客户端返回结果..............................."+res);
				if(res != null && res[0] != null&&!res[0].equals("")){
					result=res[0].toString();
				}
			}
		}catch(Exception e){
			throw new Exception("error:" + e.getMessage() , e);
		}
		return result;
	}
	
	
	public static void main(String[] args) throws Exception {
		
		WebServiceClient client = new WebServiceClient(); 
		String json = client.invoke("sayHello", "测试");
		System.out.println(json ); 
		
	}
}
